/**
 * @(#)HistoryProcessInstanceVO.java Nov 20, 2009
 *   
 * Copyright  (c) 2009-2010 Wilmar IT Service,  Inc.   
 * All  rights  reserved.   
 * TODO
 */

package com.wilmar.process.entity.vo;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.jbpm.api.ProcessInstance;
import org.jbpm.api.history.HistoryProcessInstance;

import com.wilmar.process.core.ProcessSystemDeclareKeys;

/**
 * @author makubex Email:makubexnet@gmail.com
 * @version 1.0.0
 */
public class HistoryProcessInstanceVO extends AbstractValueObject implements
		ValueObject, Comparable<HistoryProcessInstanceVO> {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 2591354647159084862L;

	private String startTime;
	private Date d_startTime;
	private Date d_endTime;
	private String endTime;
	private String key;
	private String processInstanceId;
	private String processInstanceKey;
	private String state;
	private Long duration;
	private String processDefinitionId;
	private String processDefinitionName;
	private String instanceStarter;
	private Long logId;
	private Set<String> activityNames;

	private boolean stopAble;
	private boolean suspendAble;
	private boolean resumeAble;

	/**
	 * 从一个HistoryProcessInstance和一个对应的起始日志对象构建该VO对象
	 * 
	 * @param history
	 *            历史流程实例信息
	 * @param log
	 *            起始日志
	 */
	public HistoryProcessInstanceVO(HistoryProcessInstance history,
			ProcessLogVO log) {
		this.logId = log.getId();
		SimpleDateFormat sdf = new SimpleDateFormat(
				ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT);
		this.d_endTime = history.getEndTime();
		this.d_startTime = history.getStartTime();
		if (history.getStartTime() != null)
			this.startTime = sdf.format(history.getStartTime());
		else
			this.startTime = "";
		if (history.getEndTime() != null)
			this.endTime = sdf.format(history.getEndTime());
		else
			this.endTime = "";
		if (history.getKey() != null)
			this.key = history.getKey();
		else
			this.key = "";
		this.processDefinitionName = log.getProcessDefinitionName();
		this.instanceStarter = log.getInstanceStarter();
		this.processInstanceId = history.getProcessInstanceId();
		this.state = history.getState();
		if (history.getDuration() != null)
			this.duration = history.getDuration();
		else
			this.duration = 0L;
		this.processDefinitionId = history.getProcessDefinitionId();

		this.stopAble = false;
		this.suspendAble = false;
		this.resumeAble = false;
		this.activityNames = new HashSet<String>();
	}

	/**
	 * 完整构造函数
	 * 
	 * @param history
	 *            历史信息
	 * @param pi
	 *            运行中实例信息
	 * @param log
	 *            启动日志信息
	 */
	public HistoryProcessInstanceVO(HistoryProcessInstance history,
			ProcessInstance pi, ProcessLogVO log) {
		this.logId = log.getId();
		SimpleDateFormat sdf = new SimpleDateFormat(
				ProcessSystemDeclareKeys.SYSTEM_LONG_DATE_FORMAT
						+ ProcessSystemDeclareKeys.SPACE
						+ ProcessSystemDeclareKeys.SYSTEM_LONG_TIME_FORMAT);
		this.d_endTime = history.getEndTime();
		this.d_startTime = history.getStartTime();
		if (history.getStartTime() != null)
			this.startTime = sdf.format(history.getStartTime());
		else
			this.startTime = "";
		if (history.getEndTime() != null)
			this.endTime = sdf.format(history.getEndTime());
		else
			this.endTime = "";
		if (history.getKey() != null)
			this.key = history.getKey();
		else
			this.key = "";
		this.processDefinitionName = log.getProcessDefinitionName();
		this.instanceStarter = log.getInstanceStarter();
		this.processInstanceId = history.getProcessInstanceId();
		this.state = history.getState();
		if (history.getDuration() != null)
			this.duration = history.getDuration();
		else
			this.duration = 0L;
		this.processDefinitionId = history.getProcessDefinitionId();
		if (pi != null) {
			if (pi.getKey() != null)
				this.processInstanceKey = pi.getKey();
			else
				this.processInstanceKey = "";
			if (pi.isEnded())
				this.stopAble = false;
			else
				this.stopAble = true;
			if (pi.isSuspended()) {
				this.suspendAble = false;
				this.resumeAble = true;
			} else {
				this.suspendAble = true;
				this.resumeAble = false;
			}
			this.activityNames = pi.findActiveActivityNames();
		} else {
			this.stopAble = false;
			this.suspendAble = false;
			this.resumeAble = false;
			this.activityNames = new HashSet<String>();
		}
	}

	/**
	 * 默认构造函数
	 */
	public HistoryProcessInstanceVO() {
		super();
	}

	/**
	 * @return the startTime
	 */
	public String getStartTime() {
		return startTime;
	}

	/**
	 * @param startTime
	 *            the startTime to set
	 */
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	/**
	 * @return the endTime
	 */
	public String getEndTime() {
		return endTime;
	}

	/**
	 * @param endTime
	 *            the endTime to set
	 */
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	/**
	 * @return the key
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @param key
	 *            the key to set
	 */
	public void setKey(String key) {
		this.key = key;
	}

	/**
	 * @return the processInstanceId
	 */
	public String getProcessInstanceId() {
		return processInstanceId;
	}

	/**
	 * @param processInstanceId
	 *            the processInstanceId to set
	 */
	public void setProcessInstanceId(String processInstanceId) {
		this.processInstanceId = processInstanceId;
	}

	/**
	 * @return the state
	 */
	public String getState() {
		return state;
	}

	/**
	 * @param state
	 *            the state to set
	 */
	public void setState(String state) {
		this.state = state;
	}

	/**
	 * @return the duration
	 */
	public Long getDuration() {
		return duration;
	}

	/**
	 * @param duration
	 *            the duration to set
	 */
	public void setDuration(Long duration) {
		this.duration = duration;
	}

	/**
	 * @return the processDefinitionId
	 */
	public String getProcessDefinitionId() {
		return processDefinitionId;
	}

	/**
	 * @param processDefinitionId
	 *            the processDefinitionId to set
	 */
	public void setProcessDefinitionId(String processDefinitionId) {
		this.processDefinitionId = processDefinitionId;
	}

	/**
	 * @return the processDefinitionName
	 */
	public String getProcessDefinitionName() {
		return processDefinitionName;
	}

	/**
	 * @param processDefinitionName
	 *            the processDefinitionName to set
	 */
	public void setProcessDefinitionName(String processDefinitionName) {
		this.processDefinitionName = processDefinitionName;
	}

	/**
	 * @return the instanceStarter
	 */
	public String getInstanceStarter() {
		return instanceStarter;
	}

	/**
	 * @param instanceStarter
	 *            the instanceStarter to set
	 */
	public void setInstanceStarter(String instanceStarter) {
		this.instanceStarter = instanceStarter;
	}

	/**
	 * @return the activityNames
	 */
	public Set<String> getActivityNames() {
		return activityNames;
	}

	/**
	 * @param activityNames
	 *            the activityNames to set
	 */
	public void setActivityNames(Set<String> activityNames) {
		this.activityNames = activityNames;
	}

	/**
	 * @return the stopAble
	 */
	public boolean isStopAble() {
		return stopAble;
	}

	/**
	 * @param stopAble
	 *            the stopAble to set
	 */
	public void setStopAble(boolean stopAble) {
		this.stopAble = stopAble;
	}

	/**
	 * @return the suspendAble
	 */
	public boolean isSuspendAble() {
		return suspendAble;
	}

	/**
	 * @param suspendAble
	 *            the suspendAble to set
	 */
	public void setSuspendAble(boolean suspendAble) {
		this.suspendAble = suspendAble;
	}

	/**
	 * @return the resumeAble
	 */
	public boolean isResumeAble() {
		return resumeAble;
	}

	/**
	 * @param resumeAble
	 *            the resumeAble to set
	 */
	public void setResumeAble(boolean resumeAble) {
		this.resumeAble = resumeAble;
	}

	/**
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(HistoryProcessInstanceVO o) {
		return -this.d_startTime.compareTo(o.getD_startTime());
	}

	/**
	 * @return the d_startTime
	 */
	public Date getD_startTime() {
		return d_startTime;
	}

	/**
	 * @param dStartTime
	 *            the d_startTime to set
	 */
	public void setD_startTime(Date dStartTime) {
		d_startTime = dStartTime;
	}

	/**
	 * @return the d_endTime
	 */
	public Date getD_endTime() {
		return d_endTime;
	}

	/**
	 * @param dEndTime
	 *            the d_endTime to set
	 */
	public void setD_endTime(Date dEndTime) {
		d_endTime = dEndTime;
	}

	/**
	 * @return the logId
	 */
	public Long getLogId() {
		return logId;
	}

	/**
	 * @param logId
	 *            the logId to set
	 */
	public void setLogId(Long logId) {
		this.logId = logId;
	}

	/**
	 * @return the processInstanceKey
	 */
	public String getProcessInstanceKey() {
		return processInstanceKey;
	}

	/**
	 * @param processInstanceKey
	 *            the processInstanceKey to set
	 */
	public void setProcessInstanceKey(String processInstanceKey) {
		this.processInstanceKey = processInstanceKey;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return "HistoryProcessInstanceVO [activityNames=" + activityNames
				+ ", d_endTime=" + d_endTime + ", d_startTime=" + d_startTime
				+ ", duration=" + duration + ", endTime=" + endTime
				+ ", instanceStarter=" + instanceStarter + ", key=" + key
				+ ", logId=" + logId + ", processDefinitionId="
				+ processDefinitionId + ", processDefinitionName="
				+ processDefinitionName + ", processInstanceId="
				+ processInstanceId + ", processInstanceKey="
				+ processInstanceKey + ", resumeAble=" + resumeAble
				+ ", startTime=" + startTime + ", state=" + state
				+ ", stopAble=" + stopAble + ", suspendAble=" + suspendAble
				+ "]";
	}

}
